<?php
/**
 * Created by IntelliJ IDEA.
 * User: wanglu
 * Date: 2017/7/19
 * Time: 14:13
 */

/**
 * Created by wanglu on 2017/7/19.
 */
class Key extends AdminBase
{

    function __construct()
    {
        parent::__construct();
        $this->load->service('user_service');
        $this->load->service('project_service');
        $this->load->service('lang_service');
        $this->load->service('key_service');
    }

    public function  keys() {

        $pid = $this->input->get("pid");
        $project = $this->project_service->get_project_by_id($pid);
        $pkeys = $this->project_service->get_project_keys($pid);

        $treeNodes = array();

        $treeNodes[] = array(
            'id'=> $project['id'],
            'pId'=> 0,
            'open'=> true,
            'name'=> $project['name'],
            'drag'=> false
        );


        foreach ($pkeys as $pkey) {
            $treeNodes[] = array(
                'id'=> $pkey['id'],
                'pId'=> $project['id'],
                'name'=> $pkey['pkey']
            );
        }

       // echo json_encode($treeNodes);
       // exit;
        $this->showView("keys", array('treeNodes'=> json_encode($treeNodes), 'project'=> $project));
    }


    /**
     * 更新排序
     */
    public function update_key_order() {
        $pid = $this->input->post("pid");
        $key_sorts =  $this->input->post("keyorders");
        $key_sorts = json_decode($key_sorts, true);

        if ($this->key_service->update_key_sort($pid, $key_sorts)) {
            die(json_encode(array("code" => 200)));
        } else {
            die(json_encode(array("code" => 201, "msg" => '操作失败')));
        }
    }

    public function delete() {
        $pid = $this->input->post("pid");
        $pkey = $this->input->post("pkey");

        if ($this->project_service->delete_key($pid, $pkey)) {
            die(json_encode(array("code" => 200)));
        } else {
            die(json_encode(array("code" => 201, "msg" => '操作失败')));
        }
    }

    public function update() {
        $id  = $this->input->post("id");
        $pid = $this->input->post("pid");
        $pkey = $this->input->post("pkey");
        $limit_length = $this->input->post("limit_length");
        $remark = $this->input->post("remark");

        if ($this->key_service->update_key($pid, $id, $pkey, $limit_length, $remark)) {
            die(json_encode(array("code" => 200)));
        } else {
            die(json_encode(array("code" => 201, "msg" => '操作失败')));
        }
    }


    public function add() {
        $pid = $this->input->post("pid");
        $pkey = $this->input->post("pkey");
        $limit_length = $this->input->post("limit_length");
        $remark = $this->input->post("remark");
        $display_order = $this->input->post("display_order");

        $kid = $this->key_service->add_key($pid, $pkey, $limit_length, $remark, $display_order);


        if (!empty($kid)) {
            die(json_encode(array("code" => 200, 'id'=> $kid)));
        } else {
            die(json_encode(array("code" => 201, "msg" => '操作失败')));
        }
    }
    
    public function update_pkey() {
        $id  = $this->input->post("id");
        $pid = $this->input->post("pid");
        $pkey = $this->input->post("pkey");

        if ($this->key_service->update_key_key($pid, $id, $pkey)) {
            die(json_encode(array("code" => 200)));
        } else {
            die(json_encode(array("code" => 201, "msg" => '操作失败')));
        }
    }


    public function get() {
        $kid  = $this->input->post("id");
        $key = $this->key_service->get_key_by_id($kid);
        echo json_encode($key);
    }

}